<?php
// +----------------------------------------------------------------------
// |CronWxInfoModel.class.php
// +----------------------------------------------------------------------
// |Data:2016-04-20 下午3:34
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Cli\Model;
use Common\Org\Http\HttpClient;
class CronWxInfoModel{

    /**
     * 获取微信用户信息(一分钟一次)
     */
    public function getWxUserInfo(){

        while(true){
            $data = S('ps_wx_user_base_info_list');

            if(empty($data)) break;

            foreach($data as $key=>$val){
                $detail = unserialize($val);
                if($detail['openid']){
                    $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$detail['access_token']}&openid={$detail['openid']}";
                    $res = https_request($url);
                    $result = json_decode($res,true);

                    $temp = array();
                    $temp['nickname'] = $result['nickname'];
                    $temp['gender'] = $result['sex'] ? $result['sex'] : 1;
                    $arr = array(
                        $result['country'],$result['province'],$result['city']
                    );
                    $temp['area'] = implode(',',$arr);
                    if($result['headimgurl']){
                        $params = parse_url($result['headimgurl']);
                        $arr = explode('/',$params['path']);
                        $url = $params['scheme'].'://'.$params['host'].'/'.$arr[1].'/'.$arr[2].'/96';
                        $faceName = $this->uploadImgForUrl($url,'face',$detail['uid']);
                        $temp['face'] = $faceName;
                    }
                    $res = M('user_info')->where('uid='.$detail['uid'])->save($temp);
                    if($res){
                        D('Wap/Info')->clearCache($detail['uid']);
                        unset($data[$key]);
                        S('ps_wx_user_base_info_list',$data);
                    }
                }
            }
        }
    }

    /**
     * 根据URL生成图片
     */
    public function uploadImgForUrl($imgUrl,$alias,$uid){
        if(empty($imgUrl) || empty($alias) || empty($uid)) return false;

        $host = C('TMPL_PARSE_STRING.__FILE__');
        $parameter['akey'] = C('UPLOAD_KEY');
        $parameter['skey'] = generateApiKey();
        $secret = get_secret($parameter['akey'],$parameter['skey']);
        static $client;
        if(!$client) $client = new HttpClient();

        $parameter['do'] = 'urlUpload';
        $parameter['do_type'] = base64_encode($alias);
        $parameter['uid'] = $uid;
        $parameter['url'] = $imgUrl;
        $parameter['operator'] = 'wx_face';

        $key = params_deal($host,$parameter);
        $parameter['sig'] = get_signature($key,$secret);
        $ret = $client->post($host,$parameter);
        $ret = json_decode($ret,true);
        if(empty($ret)){
            exit;
        }

        return $ret['file'];
    }
}
?>